
package Components.Templates;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;


import java.util.ArrayList;

import java.io.File;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.DataInputStream;
import java.io.ObjectOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.lang.ClassNotFoundException;

import java.awt.Point;

import Components.EnvVars.IO_Vars;

import Exceptions.BuildAnimationException;
/**
 * Klasse um z.b. einen teil eines tileset in eine animation zu schieben, und sie halt einfach speichern zu können
 * außerdem kann man sie Starten Stopen und diverses anderes
 * @author Robin
 */
public class Entity_Animation extends BasicGameState{
    
    private String name;
    private File fileAnim;
    private int i = 0;
    private int x;
    private int y;
    private float scaleW;
    private float scaleH;
    private ArrayList<Image> anim;  
    private Boolean start = false;
    
    
    /**
     * 
     * @param anim Animation , falls "Null" übergeben wird wird automatisch versucht zu laden,als ArrayList oder siehe 2ter Construktor als Array
     * @param name Name der animation (== Dateiname + .obj (evtl. auch .anim oder .pikachu)
     * @param overwrite falls dateiname vorhanden wird er bei Overwrite == true überschrieben, overwrrite == false wird nur datei geladen falls
     *	      aber nicht aufindbar wird ddie datei versucht zu schreiben, falls aber dann anim == null => 
     */
    public Entity_Animation(ArrayList<Image> anim, String name, Boolean overwrite) throws BuildAnimationException{
	
	fileAnim = new File(IO_Vars.ANIMPATH + name + IO_Vars.ANIMEXT);
	
	if(anim == null & fileAnim.exists()){
	    loadAnim();
	}else if(anim != null & fileAnim.exists() & overwrite){
	    this.anim = anim;
	    safeAnim();
	}else if(anim != null & !fileAnim.exists()){
	    
	}else{
	    throw new BuildAnimationException();
	}
    }
    
    public Entity_Animation(Image[] anim, String name, Boolean overwrite) throws BuildAnimationException{
	
    }
    
    public void setPos(Point point){
	setPos(point.x ,point.y);	
    }
    
    public void setPos(int x , int y){
	setPosX(x);
	setPosY(y);
    }
    
    public void setPosX(int x){
	this.x = x;
    }
    
    public void setPosY(int y){
	this.y = y;
    }
    
    public void stop(){
	i = 0;
	start = false;
    }
    
    public void start(){
	i = 0;
	start = true;
    }
    
    
    public void safeAnim(){
	try{
	    ObjectOutputStream objStream = new ObjectOutputStream(new FileOutputStream(this.fileAnim));
	    objStream.writeObject(this);
	    objStream.close(); 
	}catch(FileNotFoundException e){
	    
	}catch(IOException e){
	    
	}
    }
    
    public void loadAnim(){
	try{
	    ObjectInputStream objStream = new ObjectInputStream(new FileInputStream(fileAnim));
	    
	    this.anim = ((Entity_Animation)objStream.readObject()).anim;
	    this.name = ((Entity_Animation)objStream.readObject()).name;
	    this.scaleH = ((Entity_Animation)objStream.readObject()).scaleH;
	    this.scaleW = ((Entity_Animation)objStream.readObject()).scaleW;
	    
	    objStream.close(); 
	}catch(FileNotFoundException e){
	    
	}catch(IOException e){
	    
	}catch(ClassNotFoundException e){
	    
	}
	
    }

    
    

    @Override
    public int getID() {
	throw new UnsupportedOperationException("NO ID NEEDED FOR AN ANIMATION I THINK BUT WHEN WE NEED I WILL IMPLEMENT IT");
    }

    @Override
    public void init(GameContainer container, StateBasedGame game) throws SlickException {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException{   
	if(start){
	    (anim.get(i)).draw(x, y, scaleW, scaleH);
	}
    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
	i = (i+1)%anim.size();
    }
}
